//
//  Game.cpp
//  Seek&Hide
//
//  Created by 齐思 陈 on 13-10-5.
//
//

#include "Game.h"
#include "GameView.h"
#include "GameController.h"
#include "InformationLayer.h"
#include "UILayer.h"
#include "LoadingLayer.h"

F_SYNTHESIZE_IMPLEMENT(GameView*, m_pGameView, GameView, Game);
F_SYNTHESIZE_IMPLEMENT(GameController*, m_pGameController, GameController, Game);
F_SYNTHESIZE_IMPLEMENT(InformationLayer*, m_pInformationLayer, InformationLayer, Game);
F_SYNTHESIZE_IMPLEMENT(UILayer*, m_pUILayer, UILayer, Game);
F_SYNTHESIZE_IMPLEMENT(LoadingLayer*, m_pLoadingLayer, LoadingLayer, Game);

Game::Game() : m_pGameController(NULL), m_pUILayer(NULL), m_pInformationLayer(NULL), m_pGameView(NULL), m_pLoadingLayer(NULL) {
}

Game::~Game() {
    CC_SAFE_RELEASE(m_pGameView);
    CC_SAFE_RELEASE(m_pGameController);
    CC_SAFE_RELEASE(m_pInformationLayer);
    CC_SAFE_RELEASE(m_pUILayer);
    CC_SAFE_RELEASE(m_pLoadingLayer);
}

static Game* m_pSharedInstance;

Game* Game::sharedInstance()
{
    if (m_pSharedInstance == NULL) {
        m_pSharedInstance = new Game();
        m_pSharedInstance->init();
        m_pSharedInstance->autorelease();
    }
    
    return m_pSharedInstance;
}

bool Game::init() {

    if (!CCScene::init()) {
        return false;
    }
    
    m_eGameState = GameStateBegin;
    m_bPaused = false;
    m_fGameTime = 0;
    m_nRoundNum = 0;
    
    schedule(schedule_selector(Game::update));
    
	return true;
}

void Game::update(float dt)
{
    switch (m_eGameState) {
    
        case GameStateWaiting:
        {
            //等待玩家连接。连接完成发送消息变成begin。
            break;
        }
        case GameStateBegin:
        {
            setLoadingLayer(LoadingLayer::create());
            m_eGameState = GameStateLoading;
            break;
        }
        case GameStateLoading:
        {
            m_eGameState = GameStateLoadingEnd;
            break;
        }
        case GameStateLoadingEnd:
        {
            setGameController(GameController::create());
            
            setGameView(GameView::create());
            addChild(getGameView());
            
            setInformationLayer(InformationLayer::create());
            addChild(getInformationLayer(), 1);
            
            m_eGameState = GameStateGame;
            break;
        }
        case GameStateRestart:
        {
            break;
        }
        case GameStateGame:
        {
            getGameController()->update(dt);
            getGameView()->update(dt);
            break;
        }
        case GameStatePause:
        {
            break;
        }
        case GameStateEnd:
        {
            break;
        }
        default:
            break;
    }
    
    if (!m_bPaused) {
        m_fGameTime += dt;
    }
}

void Game::PauseGame()
{
    m_bPaused = true;
    m_eGameState = GameStatePause;
}

void Game::ResumeGame()
{
    m_bPaused = false;
    m_eGameState = GameStateGame;
}
